library(ggplot2)
library(ggmap)
library(usmap)
library(mapdata)
library(ggthemes)
library(dplyr)
library(RColorBrewer)
library(viridis)
library(maps)
library(cowplot) # for plot_grid() function - good to arrange multiple plots into a grid
library(grid)
library(gridGraphics)
library(patchwork)
library(gridExtra)
library(colorspace)


library(ggpubr)
require(maps)
require(viridis)

library(drat)
addRepo("geanders")
#install.packages("hurricaneexposuredata")

library(hurricaneexposure)
library(hurricaneexposuredata)
data("hurr_tracks")
data("rain")
data("flood")

BASEPATH<-Sys.getenv("DATAPATH")
OVERLEAF<-Sys.getenv("OVERLEAF")

if(BASEPATH==""){
  Sys.setenv(DATAPATH="C://Users//wb520443//Dropbox//Research Projects//Texas_Crime_Climate//Replication//")
} 
if(OVERLEAF==""){
  Sys.setenv(OVERLEAF="C://Users//wb520443//Dropbox//Research Projects//Texas_Crime_Climate//Replication//")
}

BASEPATH<-Sys.getenv("DATAPATH")
OVERLEAF<-Sys.getenv("OVERLEAF")

storm.sample <-read.csv(paste0(BASEPATH,"data//figdat//storm_sample.csv"), header = TRUE)
migration.sample <-read.csv(paste0(BASEPATH,"data//figdat//migration_sample.csv"), header = TRUE)
storm.sample.flood <-read.csv(paste0(BASEPATH,"data//figdat//storm_sample_flood.csv"), header = TRUE)
storm.sample.wind <-read.csv(paste0(BASEPATH,"data//figdat//storm_sample_wind.csv"), header = TRUE)
gdp.sample <-read.csv(paste0(BASEPATH,"data//figdat//county_gdp.csv"), header = TRUE)
storm.year.sample <-read.csv(paste0(BASEPATH,"data//figdat//county_to_migration_storm.csv"), header = TRUE)
nonstorm.year.sample <-read.csv(paste0(BASEPATH,"data//figdat//county_to_migration_nonstorm.csv"), header = TRUE)
highdamage.10mm <-read.csv(paste0(BASEPATH,"data//figdat//highdamage_10mm.csv"), header = TRUE)
highdamage.20mm <-read.csv(paste0(BASEPATH,"data//figdat//highdamage_20mm.csv"), header = TRUE)
highdamage.40mm <-read.csv(paste0(BASEPATH,"data//figdat//highdamage_40mm.csv"), header = TRUE)
highdamage.80mm <-read.csv(paste0(BASEPATH,"data//figdat//highdamage_80mm.csv"), header = TRUE)
highdamage.160mm <-read.csv(paste0(BASEPATH,"data//figdat//highdamage_160mm.csv"), header = TRUE)






total_storm_map<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(migration.sample),
  values = "total_storms",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Reds', 
                                   na.value = "grey90", 
                                   guide =   guide_colourbar(title.position = "top",
                                                             title.hjust=0.5,barwidth = 25,
                                                             barheight = 1.5))+
  labs(fill = "Total storm exposure in our sample")+
  theme(legend.position = c(0, 0))+
  theme(legend.direction="horizontal")+
  theme(text = element_text(size = 20))+ theme(legend.background=element_blank())
ggsave(filename = paste0(OVERLEAF,"figures//fig_1b.png"), 
       plot = total_storm_map, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)





flood_sample_map<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(storm.sample.flood),
  values = "has_storm",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Reds 2', 
                                   na.value = "grey90", 
                                   guide =   guide_colourbar(title.position = "top",title.hjust=0.5,barwidth = 25,barheight = 1.5))+
  theme(legend.position = "none")
ggsave(filename = paste0(OVERLEAF,"figures//fig_si1a.png"), 
       plot = flood_sample_map, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)


wind_sample_map<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(storm.sample.wind),
  values = "has_storm",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Reds 2', 
                                   na.value = "grey90", 
                                   guide =   guide_colourbar(title.position = "top",title.hjust=0.5,barwidth = 25,barheight = 1.5))+
  theme(legend.position = "none")
ggsave(filename = paste0(OVERLEAF,"figures//fig_si1b.png"), 
       plot = wind_sample_map, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)



gdp_map<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(gdp.sample),
  values = "gdp2019",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Greens 2', 
                                   na.value = "grey90", 
                                   guide =   guide_colourbar(title.position = "top",
                                                             title.hjust=0.5,barwidth = 25,
                                                             barheight = 1.5))+
  labs(fill = "Log of county GDP in 2019")+
  theme(legend.position = c(0, 0))+
  theme(legend.direction="horizontal")+
  theme(text = element_text(size = 20))+ theme(legend.background=element_blank())
ggsave(filename = paste0(OVERLEAF,"figures//fig_1a.png"), 
       plot = gdp_map, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)




country_storm_map<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(storm.year.sample),
  values = "migration",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Oranges', 
                                   na.value = "grey90", 
                                   breaks = c(0,50,100,150,200,250),
                                   limits=c(0, 250),
                                   guide =   guide_colourbar(title.position = "top",
                                                             title.hjust=0.5,
                                                             barwidth = 25,
                                                             barheight = 1.5))+
  labs(fill = "Avg. in-migration, storm years")+
  theme(legend.position = c(0, 0))+
  theme(legend.direction="horizontal")+
  theme(text = element_text(size = 20))+ theme(legend.background=element_blank())
ggsave(filename = paste0(OVERLEAF,"figures//fig_1e.png"), 
       plot = country_storm_map, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)

country_nonstorm_map<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(nonstorm.year.sample),
  values = "migration",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Oranges', 
                                   na.value = "grey90", 
                                   breaks = c(0,50,100,150,200,250),
                                   limits=c(0, 250),
                                   guide =   guide_colourbar(title.position = "top",
                                                             title.hjust=0.5,
                                                             barwidth = 25,
                                                             barheight = 1.5))+
  labs(fill = "Avg. in-migration, non-storm years")+
  theme(legend.position = c(0, 0))+
  theme(legend.direction="horizontal")+
  theme(text = element_text(size = 20))+ theme(legend.background=element_blank())
ggsave(filename = paste0(OVERLEAF,"figures//fig_1f.png"), 
       plot = country_nonstorm_map, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)


highdamage_10mm<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(highdamage.10mm),
  values = "highdamage",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Reds 2', 
                                   na.value = "grey90", 
                                   guide =   guide_colourbar(title.position = "top",title.hjust=0.5,barwidth = 25,barheight = 1.5))+
  theme(legend.position = "none")
ggsave(filename = paste0(OVERLEAF,"figures//fig_si2a.png"), 
       plot = highdamage_10mm, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)



highdamage_20mm<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(highdamage.20mm),
  values = "highdamage",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Reds 2', 
                                   na.value = "grey90", 
                                   guide =   guide_colourbar(title.position = "top",title.hjust=0.5,barwidth = 25,barheight = 1.5))+
  theme(legend.position = "none")
ggsave(filename = paste0(OVERLEAF,"figures//fig_si2b.png"), 
       plot = highdamage_20mm, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)


highdamage_40mm<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(highdamage.40mm),
  values = "highdamage",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Reds 2', 
                                   na.value = "grey90", 
                                   guide =   guide_colourbar(title.position = "top",title.hjust=0.5,barwidth = 25,barheight = 1.5))+
  theme(legend.position = "none")
ggsave(filename = paste0(OVERLEAF,"figures//fig_si2c.png"), 
       plot = highdamage_40mm, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)


highdamage_80mm<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(highdamage.80mm),
  values = "highdamage",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Reds 2', 
                                   na.value = "grey90", 
                                   guide =   guide_colourbar(title.position = "top",title.hjust=0.5,barwidth = 25,barheight = 1.5))+
  theme(legend.position = "none")
ggsave(filename = paste0(OVERLEAF,"figures//fig_si2d.png"), 
       plot = highdamage_80mm, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)


highdamage_160mm<-plot_usmap(
  regions = c("counties"),
  exclude = c("AK","HI"),
  data = data.frame(highdamage.160mm),
  values = "highdamage",
  color="grey90"
)+scale_fill_continuous_sequential(palette = 'Reds 2', 
                                   na.value = "grey90", 
                                   guide =   guide_colourbar(title.position = "top",title.hjust=0.5,barwidth = 25,barheight = 1.5))+
  theme(legend.position = "none")
ggsave(filename = paste0(OVERLEAF,"figures//fig_si2e.png"), 
       plot = highdamage_160mm, 
       width = 14.5,
       height = 9,
       units = c("in"),
       dpi = 300)
































